﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using Pai.LiveStation.Data;
using System.Data.Linq;
namespace Pai.LiveStation.Web
{
    /// <summary>
    /// Summary description for BroadcastService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class BroadcastService : System.Web.Services.WebService
    {

        [WebMethod]
        public XmlDocument GetBroadcast()
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                Broadcast broadcast = dc.Broadcasts.Where(x => x.ReleaseDate <= DateTime.Now)
                    .OrderByDescending(y => y.ReleaseDate).FirstOrDefault();

                if (broadcast == null)
                    return null;

                XmlDocument doc = new XmlDocument();
                //XmlNode dec = doc.CreateNode(XmlNodeType.
                XmlElement root = doc.CreateElement("Broadcast");
                XmlAttribute type = doc.CreateAttribute("Type");
                type.Value = broadcast.BroadcastType.Name;
                root.Attributes.Append(type);

                doc.AppendChild(root);
                EntitySet<BroadcastsMedia> broadcastItems = broadcast.BroadcastsMedias;
                foreach (BroadcastsMedia broadcastItem in broadcastItems)
                {
                    XmlElement elem = doc.CreateElement("Media");
                    XmlAttribute id = doc.CreateAttribute("id");
                    id.Value = broadcastItem.Media.ID.ToString();
                    elem.Attributes.Append(id);
                    XmlAttribute attr = doc.CreateAttribute("Rank");
                    attr.Value = broadcastItem.Rank.ToString();
                    elem.Attributes.Append(attr);

                        XmlElement name = doc.CreateElement("name");
                        name.InnerText = broadcastItem.Media.Name.ToString();
                        elem.AppendChild(name);

                        XmlElement description = doc.CreateElement("description");
                        description.InnerText = broadcastItem.Media.Description.ToString();
                        elem.AppendChild(description);

                        XmlElement location = doc.CreateElement("location");
                        location.InnerText = broadcastItem.Media.Location.ToString();
                        elem.AppendChild(location);

                        XmlElement copyright = doc.CreateElement("copyright");
                        copyright.InnerText = broadcastItem.Media.Copyright.ToString();
                        elem.AppendChild(copyright);

                        XmlElement dateCreated = doc.CreateElement("dateCreated");
                        dateCreated.InnerText = broadcastItem.Media.DateCreated.ToString();
                        elem.AppendChild(dateCreated);

                        XmlElement datePublished = doc.CreateElement("datePublished");
                        datePublished.InnerText = broadcastItem.Media.DatePublished.ToString();
                        elem.AppendChild(datePublished);

                        XmlElement mType = doc.CreateElement("type");
                        mType.InnerText = broadcastItem.Media.MediaType.Name;
                        elem.AppendChild(mType);

                    root.AppendChild(elem);
                }

                return doc;


            }
        }
    }
}
